package com.my.screens;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
import com.my.drinkinggame.Main;
import com.my.drinkinggame.Renderer;
import com.my.drinkinggame.StaticFonts;
import com.my.drinkinggame.StaticSprites;

public class NewGame extends Screen{
	boolean available;
	Main main;
	public OrthographicCamera cam;
	Vector3 translationVector;
	public boolean resetButtons;
	public float xPos;
	public float yPos;
	public float timer;
	public Sprite activeCancel;
	public Sprite activeStart;
	public Sprite headerSprite;
	public Sprite activeGiveTake;
	public Sprite activeMyOwnCards;
	public Sprite activeNerdFact;
	public Sprite activeNeverHaveI;
	public Sprite activeStandard;
	public Sprite activeRolePlaying;
	public boolean giveTake, myOwnCards, nerdFact, neverHaveI, standard, rolePlaying;

	public NewGame(Main m) {
		cam = new OrthographicCamera();
		translationVector = new Vector3();
		main = m;
		xPos = 0;
		yPos = 0;
		timer = 0;
		resetButtons = false;
		activeCancel = StaticSprites.cancelBut;
		activeStart = StaticSprites.start;
		headerSprite = StaticSprites.startBut;
		activeGiveTake = StaticSprites.giveTakeSel;
		activeMyOwnCards = StaticSprites.myOwnCardsSel;
		activeNerdFact = StaticSprites.nerdFactsSel;
		activeNeverHaveI = StaticSprites.neverHaveISel;
		activeStandard = StaticSprites.standardSel;
		activeRolePlaying = StaticSprites.rolePlayingSel;
		/*
		activeGiveTake.setScale(2f);
		activeMyOwnCards.setScale(2f);
		activeNerdFact.setScale(2f);
		activeNeverHaveI.setScale(2f);
		activeStandard.setScale(2f);
		activeRolePlaying.setScale(2f);
		*/
		giveTake = true;
		myOwnCards = true;
		nerdFact = true;
		neverHaveI = true;
		standard = true;
		rolePlaying = true;
	}

	public void resetAll(){
		resetButtons = true;
		activeCancel = StaticSprites.cancelBut;
		activeStart = StaticSprites.start;
	}

	@Override
	public boolean keyDown(int arg0) {
		if (Input.Keys.BACK == arg0) {
			resetAll();
			main.setScreen(Main.SCREEN_MAINMENU);
		}
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyTyped(char character) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown(int x, int y, int pointer, int button) {
		translationVector.set(x,y,0);
		cam.unproject(translationVector);
		xPos = translationVector.x;
		yPos = translationVector.y;
		if(activeGiveTake.getBoundingRectangle().contains(translationVector.x, translationVector.y)){
			if(giveTake){
				activeGiveTake = StaticSprites.giveTake;
				giveTake = false;
			}
			else{
				activeGiveTake = StaticSprites.giveTakeSel;
				giveTake = true;
			}
		}
		if(activeStandard.getBoundingRectangle().contains(translationVector.x, translationVector.y)){
			if(standard){
				activeStandard = StaticSprites.standard;
				standard = false;
			}
			else{
				activeStandard = StaticSprites.standardSel;
				standard = true;
			}
		}
		if(activeNerdFact.getBoundingRectangle().contains(translationVector.x, translationVector.y)){
			if(nerdFact){
				activeNerdFact = StaticSprites.nerdFacts;
				nerdFact = false;
			}
			else{
				activeNerdFact = StaticSprites.nerdFactsSel;
				nerdFact = true;
			}
		}
		if(activeNeverHaveI.getBoundingRectangle().contains(translationVector.x, translationVector.y)){
			if(neverHaveI){
				activeNeverHaveI = StaticSprites.neverHaveI;
				neverHaveI = false;
			}
			else{
				activeNeverHaveI = StaticSprites.neverHaveISel;
				neverHaveI = true;
			}
		}
		if(activeRolePlaying.getBoundingRectangle().contains(translationVector.x, translationVector.y)){
			if(rolePlaying){
				activeRolePlaying = StaticSprites.rolePlaying;
				rolePlaying = false;
			}
			else{
				activeRolePlaying = StaticSprites.rolePlayingSel;
				rolePlaying = true;
			}
		}
		if(activeMyOwnCards.getBoundingRectangle().contains(translationVector.x, translationVector.y)){
			if(myOwnCards){
				activeMyOwnCards = StaticSprites.myOwnCards;
				myOwnCards = false;
			}
			else{
				activeMyOwnCards = StaticSprites.myOwnCardsSel;
				myOwnCards = true;
			}
		}
		resetButtons = false;
		return false;
	}

	@Override
	public boolean touchUp(int x, int y, int pointer, int button) {
		translationVector.set(x,y,0);
		cam.unproject(translationVector);
		if(activeCancel.getBoundingRectangle().contains(translationVector.x, translationVector.y)){
			resetAll();
			main.setScreen(Main.SCREEN_MAINMENU);
		}
		if(activeStart.getBoundingRectangle().contains(translationVector.x, translationVector.y)){
			//((MyDrinkingGame) main.getState(Main.SCREEN_GAME)).sim.reset();
			//((MyDrinkingGame) main.getState(Main.SCREEN_GAME)).sim.initiateCards(giveTake, myOwnCards, nerdFact, neverHaveI, standard, rolePlaying);
			((MainMenu) main.getState(Main.SCREEN_MAINMENU)).gameStarted = true;
			((MyDrinkingGame) main.getState(Main.SCREEN_GAME)).startedGame = false;
			main.setScreen(Main.SCREEN_GAME);
		}
		resetButtons = true;
		return false;
	}

	@Override
	public boolean touchDragged(int x, int y, int pointer) {
		translationVector.set(x,y,0);
		cam.unproject(translationVector);
		xPos = translationVector.x;
		yPos = translationVector.y;
		return false;
	}

	public boolean mouseMoved(int screenX, int screenY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean scrolled(int amount) {
		// TODO Auto-generated method stub
		return false;
	}

	public void updateCancelBut(){
		if(activeCancel.getBoundingRectangle().contains(xPos, yPos)){
			activeCancel = StaticSprites.cancelButSel;
		}
		else{
			activeCancel = StaticSprites.cancelBut;
		}
	}
	
	public void updateStartBut(){
		if(activeStart.getBoundingRectangle().contains(xPos, yPos)){
			activeStart = StaticSprites.startSel;
		}
		else{
			activeStart = StaticSprites.start;
		}
	}
	
	public void resetButs(){
		activeCancel = StaticSprites.cancelBut;
		activeStart = StaticSprites.start;
	}
	
	public void updateModes(){
		((MyDrinkingGame) main.getState(Main.SCREEN_GAME)).sim.giveTake = giveTake;
		((MyDrinkingGame) main.getState(Main.SCREEN_GAME)).sim.nerdFact = nerdFact;
		((MyDrinkingGame) main.getState(Main.SCREEN_GAME)).sim.neverHaveI = neverHaveI;
		((MyDrinkingGame) main.getState(Main.SCREEN_GAME)).sim.standard = standard;
		((MyDrinkingGame) main.getState(Main.SCREEN_GAME)).sim.rolePlaying = rolePlaying;
		((MyDrinkingGame) main.getState(Main.SCREEN_GAME)).sim.myOwnCards = myOwnCards;
	}
	@Override
	public void update() {
		updateCancelBut();
		updateStartBut();
		updateModes();
		if(resetButtons){
			resetButs();
		}

	}

	@Override
	public void render() {
		Renderer.getInstance().renderNewGame(this);

	}

	public void renderNewGame(SpriteBatch sb){
		StaticSprites.newGameBG.draw(sb);
		StaticFonts.comic_48.setScale(1);
		headerSprite.setPosition(StaticSprites.addedCardsBG.getX()+
				StaticSprites.addedCardsBut.getWidth()/2, 
				StaticSprites.addedCardsBG.getY()+StaticSprites.addedCardsBG.getHeight()/1.1f);
		headerSprite.draw(sb);
		activeGiveTake.setPosition(StaticSprites.newGameBG.getX()+StaticFonts.comic_48.getBounds("     ").width, 
				cam.position.y+Gdx.graphics.getHeight()/2.9f-
				StaticSprites.giveTake.getHeight()/1.32f-
				StaticSprites.giveTake.getHeight()*4f);
		activeGiveTake.draw(sb);
		activeStandard.setPosition(StaticSprites.newGameBG.getX()+StaticFonts.comic_48.getBounds("     ").width, 
				cam.position.y+Gdx.graphics.getHeight()/2.9f-
				StaticSprites.giveTake.getHeight()/1.32f-
				StaticSprites.giveTake.getHeight()*8f);
		activeStandard.draw(sb);
		activeNerdFact.setPosition(StaticSprites.newGameBG.getX()+StaticFonts.comic_48.getBounds("     ").width, 
				cam.position.y+Gdx.graphics.getHeight()/2.9f-
				StaticSprites.giveTake.getHeight()/1.32f-
				StaticSprites.giveTake.getHeight()*12f);
		activeNerdFact.draw(sb);
		activeNeverHaveI.setPosition(StaticSprites.newGameBG.getX()+StaticFonts.comic_48.getBounds("     ").width, 
				cam.position.y+Gdx.graphics.getHeight()/2.9f-
				StaticSprites.giveTake.getHeight()/1.32f-
				StaticSprites.giveTake.getHeight()*16f);
		activeNeverHaveI.draw(sb);
		activeRolePlaying.setPosition(StaticSprites.newGameBG.getX()+StaticFonts.comic_48.getBounds("     ").width, 
				cam.position.y+Gdx.graphics.getHeight()/2.9f-
				StaticSprites.giveTake.getHeight()/1.32f-
				StaticSprites.giveTake.getHeight()*20f);
		activeRolePlaying.draw(sb);
		activeMyOwnCards.setPosition(StaticSprites.newGameBG.getX()+StaticFonts.comic_48.getBounds("     ").width, 
				cam.position.y+Gdx.graphics.getHeight()/2.9f-
				StaticSprites.giveTake.getHeight()/1.32f-
				StaticSprites.giveTake.getHeight()*24f);
		activeMyOwnCards.draw(sb);
		
		
		
		activeStart.setPosition(cam.position.x-StaticSprites.start.getWidth()/2, 
				cam.position.y-Gdx.graphics.getHeight()/2.3f);
		activeStart.draw(sb);
		activeCancel.setPosition(cam.position.x-StaticSprites.cancelBut.getWidth()/2+StaticSprites.cancelBut.getWidth(), 
				cam.position.y-Gdx.graphics.getHeight()/2.3f);
		activeCancel.draw(sb);
	}

	@Override
	public void enter() {
		Gdx.input.setInputProcessor(this);
		Gdx.input.setCatchBackKey(true);
	}

	@Override
	public boolean touchMoved(int x, int y) {
		// TODO Auto-generated method stub
		return false;
	}

}
